package com.seatiger.structure.recursion;

/**
 * 递归求和
 */
public class ArraySum {
    public static int sum(int[] arr) {
        return sum(arr,0);
    }

    //计算arr[l..n)这个区间所有数组的和
    private static int sum(int[] arr, int l) {
        //递归的终止条件
        if (l == arr.length) {
            return 0;
        }
        return arr[l] + sum(arr,l+1);
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3,4,5};
        System.out.println(sum(nums));
    }
}
